package com.gzdh.dishrecognition.ui.dialog;

import static com.dhkj.web.utils.DataUtils.type;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.alipay.iot.bpaas.api.utils.StringUtil;
import com.dhkj.web.bean.Bmcm;
import com.dhkj.web.bean.Bmzl;
import com.dhkj.web.utils.MyObserverRes;
import com.dhkj.web.utils.MyObserverUZip;
import com.dhkj.web.utils.RequestUtils;
import com.github.promeg.pinyinhelper.Pinyin;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.emu.WeekMenuBakTEmus;
import com.gzdh.dishrecognition.model.HosMenuDto;
import com.gzdh.dishrecognition.model.MenuLBDto;
import com.gzdh.dishrecognition.model.NWeekMenuBakDto;
import com.gzdh.dishrecognition.ui.adapter.LettersAdapter;
import com.gzdh.dishrecognition.ui.adapter.paicai.LeftMenuAdapter;
import com.gzdh.dishrecognition.ui.adapter.paicai.PaiCaiLeftMenuLbAdapter;
import com.gzdh.dishrecognition.ui.adapter.paicai.PaiCaiRightZlAdapter;
import com.gzdh.dishrecognition.ui.adapter.paicai.RightMenuAdapter;
import com.gzdh.dishrecognition.ui.view.GridViewForScrollView;
import com.gzdh.dishrecognition.ui.view.MyGridSpacingItemDecoration;
import com.gzdh.dishrecognition.util.SystemUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class PaiCaiDialog extends DialogFragment {
    private static final String TAG = "PaiCaiDialog";
    private Context mContext;

    private TextView bmcmBut;
    private TextView savedmBut;

    private PaiCaiLeftMenuLbAdapter paiCaiLeftMenuLbAdapter;

    private LeftMenuAdapter leftMenuAdapter;

    private RightMenuAdapter rightMenuAdapter;

    private PaiCaiRightZlAdapter paiCaiRightZlAdapter;

    private List<Bmcm> bmcmList = new ArrayList<>();

    private int table;

    private List<String> savedmList;

    private String savedm = "";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        setRetainInstance(true);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = inflater.getContext();
        View view = getViews();
        getDialog().getWindow().getDecorView().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                InputMethodManager manager = (InputMethodManager) getActivity()
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (getDialog().getCurrentFocus() != null
                            && getDialog().getCurrentFocus().getWindowToken() != null) {
                        manager.hideSoftInputFromWindow(getDialog().getCurrentFocus()
                                .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
                return false;
            }
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        RequestUtils.getInfo(mContext, SystemUtils.getSerialNumber(), table, new MyObserverUZip<JSONObject>(mContext, type(JSONObject.class)) {
            @Override
            public void onSuccess(JSONObject result) {
                List<MenuLBDto> menuLBDtoList = result.getJSONArray("menuLBDtoList").toJavaList(MenuLBDto.class);
                paiCaiLeftMenuLbAdapter.setList(menuLBDtoList);


                List<HosMenuDto> hosMenuDtoList = result.getJSONArray("hosMenuDtoList").toJavaList(HosMenuDto.class);
                for (HosMenuDto goo : hosMenuDtoList) {
                    goo.setNamePinyin(pinYinS(goo.getName()));
                }
                leftMenuAdapter.setList(hosMenuDtoList);
//                leftMenuAdapter.setMenuLBDto(menuLBDtoList.get(0));
                leftMenuAdapter.showNotify();

                List<Bmzl> bmzlList = result.getJSONArray("bmzlList").toJavaList(Bmzl.class);
                paiCaiRightZlAdapter.setList(bmzlList);
                if (bmzlList.size() > 0) {
                    rightMenuAdapter.setZl(bmzlList.get(0).getBm());
                }


                bmcmList = result.getJSONArray("bmcmList").toJavaList(Bmcm.class);
                if (bmcmList.size() > 0) {
                    rightMenuAdapter.setDinnerBm(bmcmList.get(0).getBm());
                }
                rightMenuAdapter.showNotify();
                savedmList = result.getJSONArray("savedmList").toJavaList(String.class);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {

            }
        });
    }


    private void getWeekMenu() {
        RequestUtils.getWeekMenuBak(mContext, SystemUtils.getSerialNumber(),
                table, savedm, new SimpleDateFormat("yyyy-MM-dd").format(new Date()), new MyObserverUZip<JSONObject>(mContext, type(JSONObject.class)) {
                    @Override
                    public void onSuccess(JSONObject result) {
                        List<NWeekMenuBakDto> nWeekMenuBakDtoList = result.getJSONArray("nWeekMenuBakDtoList").toJavaList(NWeekMenuBakDto.class);
                        rightMenuAdapter.setList(nWeekMenuBakDtoList);
                        rightMenuAdapter.showNotify();
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {

                    }
                });
    }

    private View getViews() {

        LinearLayout mainLinear = new LinearLayout(mContext);
        mainLinear.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mainLinear.addView(getTitle());
        mainLinear.setOrientation(LinearLayout.VERTICAL);
        View lineView = new View(mContext);
        lineView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
        lineView.setBackgroundColor(Color.rgb(220, 220, 220));
        mainLinear.addView(lineView);

        mainLinear.addView(getBody());
        return mainLinear;

    }


    private View getTitle() {


        RelativeLayout relativeLayout = new RelativeLayout(mContext);
        relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        TextView textView = new TextView(mContext);
        textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        textView.setText("快速排菜");
        textView.setPadding(15, 15, 15, 15);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
        relativeLayout.addView(textView);

        LinearLayout rigLinear = new LinearLayout(mContext);
        RelativeLayout.LayoutParams rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rvParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        rvParams.addRule(RelativeLayout.CENTER_VERTICAL);
        rigLinear.setGravity(Gravity.CENTER);
        rigLinear.setLayoutParams(rvParams);
        rigLinear.setOrientation(LinearLayout.HORIZONTAL);
        relativeLayout.addView(rigLinear);


        textView = new TextView(mContext);
        textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        textView.setText("表：");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        rigLinear.addView(textView);

        TextView saveBut2 = new TextView(mContext);
        rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rvParams.setMargins(0, 0, 20, 0);
        saveBut2.setLayoutParams(rvParams);
        saveBut2.setText(WeekMenuBakTEmus.getByValue(table).getName());
        saveBut2.setTextColor(Color.WHITE);
        saveBut2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        saveBut2.setPadding(20, 8, 20, 8);
        saveBut2.setBackground(getResources().getDrawable(R.drawable.bg_btn_pay));
        rigLinear.addView(saveBut2);
        saveBut2.setOnClickListener(view -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("请选择数据表");
            final Map<String, Integer> map = new LinkedHashMap<>();
            for (WeekMenuBakTEmus goo : WeekMenuBakTEmus.values()) {
                map.put(goo.getName(), goo.getValue());
            }
            final String[] keysTemp = new String[WeekMenuBakTEmus.values().length];
            final String[] keys = map.keySet().toArray(keysTemp);
            builder.setItems(keys, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    int state = map.get(keys[which]);
//                    SysConstConfig.getInstance().setSurfaceModel(ModelEmus.getByValue(state));
//                    showState();


                }
            });
            builder.show();
        });

        textView = new TextView(mContext);
        textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        textView.setText("菜谱：");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        rigLinear.addView(textView);
        savedmBut = new TextView(mContext);
        rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rvParams.setMargins(0, 0, 20, 0);
        savedmBut.setLayoutParams(rvParams);
        savedmBut.setText("请选择");
        savedmBut.setTextColor(Color.WHITE);
        savedmBut.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        savedmBut.setPadding(20, 8, 20, 8);
        savedmBut.setBackground(getResources().getDrawable(R.drawable.bg_btn_pay));
        rigLinear.addView(savedmBut);
        savedmBut.setOnClickListener(view -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("请选择菜谱");
            final Map<String, String> map = new LinkedHashMap<>();
            for (String goo : savedmList) {
                map.put(goo, goo);
            }
            final String[] keysTemp = new String[savedmList.size()];
            final String[] keys = map.keySet().toArray(keysTemp);
            builder.setItems(keys, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String state = map.get(keys[which]);
                    savedm = state;
                    savedmBut.setText(savedm.trim());
                    getWeekMenu();
                }
            });
            builder.show();
        });

        textView = new TextView(mContext);
        textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        textView.setText("餐次：");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        rigLinear.addView(textView);


        bmcmBut = new TextView(mContext);
        rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rvParams.setMargins(0, 0, 20, 0);
        bmcmBut.setLayoutParams(rvParams);
        bmcmBut.setText("早餐");
        bmcmBut.setTextColor(Color.WHITE);
        bmcmBut.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        bmcmBut.setPadding(20, 8, 20, 8);
        bmcmBut.setBackground(getResources().getDrawable(R.drawable.bg_btn_pay));
        rigLinear.addView(bmcmBut);
        bmcmBut.setOnClickListener(view -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("餐次选择");
            final Map<String, String> map = new LinkedHashMap<>();
            for (Bmcm goo : bmcmList) {
                map.put(goo.getName(), goo.getBm());
            }
            final String[] keysTemp = new String[bmcmList.size()];
            final String[] keys = map.keySet().toArray(keysTemp);
            builder.setItems(keys, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String state = map.get(keys[which]);
                    bmcmBut.setText(bmcmList.get(which).getName().trim());
                    rightMenuAdapter.setDinnerBm(state);
                    rightMenuAdapter.showNotify();
                }
            });
            builder.show();
        });

        TextView saveBut = new TextView(mContext);
        rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rvParams.setMargins(20, 0, 20, 0);
        saveBut.setLayoutParams(rvParams);
        saveBut.setText("保存");
        saveBut.setTextColor(Color.WHITE);
        saveBut.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        saveBut.setPadding(20, 8, 20, 8);
        saveBut.setBackground(getResources().getDrawable(R.drawable.bg_btn_pay));
        rigLinear.addView(saveBut);

        saveBut.setOnClickListener(view -> {
            List<NWeekMenuBakDto> nWeekMenuBakDtoList = rightMenuAdapter.submitWeekMenu();
            if (nWeekMenuBakDtoList == null) {
                MsgDialog msgDialog = new MsgDialog(mContext);
                msgDialog.show();
                msgDialog.showMsg("你的操作有误，请核对后操作");
                return;
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("nWeekMenuBakDtoList", nWeekMenuBakDtoList);
            jsonObject.put("savedm", savedm);
            jsonObject.put("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            jsonObject.put("table", table);
            jsonObject.put("deviceId", SystemUtils.getSerialNumber());

            RequestUtils.saveWeekMenuBak(mContext, jsonObject, new MyObserverRes<String>(mContext) {
                @Override
                public void onSuccess(String result) {
                    System.out.println("");
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {

                }
            });
        });

        ImageView closeBut = new ImageView(mContext);
        closeBut.setLayoutParams(new RelativeLayout.LayoutParams(60, 60));
        closeBut.setBackground(getResources().getDrawable(R.drawable.icon_close_ungrand_selector));
        rigLinear.addView(closeBut);
        closeBut.setOnClickListener(view -> {
            dismiss();
        });
        return relativeLayout;
    }

    private View getBody() {
        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setBackgroundColor(Color.WHITE);
        linearLayout.addView(getLeftView());
        linearLayout.addView(getRightView());
        return linearLayout;
    }

    private View getLeftView() {
        LinearLayout linearLayout = new LinearLayout(mContext);
        LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
        linearParams.setMargins(10, 10, 10, 10);
        linearLayout.setLayoutParams(linearParams);
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        LinearLayout topRv = new LinearLayout(mContext);
        topRv.setOrientation(LinearLayout.HORIZONTAL);
        topRv.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        linearLayout.addView(topRv);

        TextView textView = new TextView(mContext);
        RelativeLayout.LayoutParams rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rvParams.addRule(RelativeLayout.CENTER_VERTICAL);

        textView.setId(View.generateViewId());
        textView.setLayoutParams(rvParams);
        textView.setText("未排菜");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        topRv.addView(textView);

        TextView menuCirculationStartItemCount = new TextView(mContext);
        rvParams = new RelativeLayout.LayoutParams(24, 24);
        rvParams.addRule(RelativeLayout.RIGHT_OF, textView.getId());
        menuCirculationStartItemCount.setId(View.generateViewId());
        menuCirculationStartItemCount.setLayoutParams(rvParams);
        menuCirculationStartItemCount.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        menuCirculationStartItemCount.setGravity(Gravity.CENTER);
        menuCirculationStartItemCount.setText("0");
        topRv.addView(menuCirculationStartItemCount);

        TextView menuCirculationStartSortText = new TextView(mContext);
        linearParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
        linearParams.setMargins(5, 0, 5, 0);
        menuCirculationStartSortText.setId(View.generateViewId());
        menuCirculationStartSortText.setLayoutParams(linearParams);
        menuCirculationStartSortText.setHint("输入简码");
        menuCirculationStartSortText.setEnabled(false);
        menuCirculationStartSortText.setPadding(5, 5, 5, 5);
        menuCirculationStartSortText.setSingleLine(true);
        topRv.addView(menuCirculationStartSortText);


        TextView menuCirculationStartReset = new TextView(mContext);
        rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rvParams.setMargins(5, 0, 5, 0);
        rvParams.addRule(RelativeLayout.CENTER_VERTICAL);
        menuCirculationStartReset.setLayoutParams(rvParams);
        menuCirculationStartReset.setText("重置");
        menuCirculationStartReset.setPadding(20, 8, 20, 8);
        menuCirculationStartReset.setTextColor(Color.WHITE);
        menuCirculationStartReset.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        menuCirculationStartReset.setBackground(getResources().getDrawable(R.drawable.bg_btn_pay));
        topRv.addView(menuCirculationStartReset);
        menuCirculationStartReset.setOnClickListener(view -> {
            leftMenuAdapter.setSearch("");
            leftMenuAdapter.showNotify();
            menuCirculationStartSortText.setText("");
        });
        TextView menuCirculationAll = new TextView(mContext);
        rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rvParams.setMargins(5, 0, 10, 0);
        rvParams.addRule(RelativeLayout.CENTER_VERTICAL);
        rvParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        menuCirculationAll.setId(View.generateViewId());
        menuCirculationAll.setLayoutParams(rvParams);
        menuCirculationAll.setText("全排菜");
        menuCirculationAll.setPadding(20, 8, 20, 8);
        menuCirculationAll.setTextColor(Color.WHITE);
        menuCirculationAll.setBackground(getResources().getDrawable(R.drawable.bg_btn_pay));
        menuCirculationAll.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        topRv.addView(menuCirculationAll);


        ScrollView scrollView = new ScrollView(mContext);
        rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rvParams.setMargins(0, 5, 0, 0);
        scrollView.setLayoutParams(rvParams);
        linearLayout.addView(scrollView);

        GridViewForScrollView menuCirculationStartSort = new GridViewForScrollView(mContext);
        menuCirculationStartSort.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        menuCirculationStartSort.setNumColumns(13);
        menuCirculationStartSort.setHorizontalSpacing(5);
        menuCirculationStartSort.setVerticalSpacing(5);
        scrollView.addView(menuCirculationStartSort);

        LettersAdapter startLettersAdapter = new LettersAdapter(getActivity());
        menuCirculationStartSort.setAdapter(startLettersAdapter);
        menuCirculationStartSort.setOnItemClickListener((parent, view, position, id) -> {
            String letter = startLettersAdapter.getLetters()[position];
            String startQueryString = menuCirculationStartSortText.getText().toString() + letter;
            menuCirculationStartSortText.setText(startQueryString);
            leftMenuAdapter.setSearch(startQueryString);
            leftMenuAdapter.showNotify();

        });


        LinearLayout menuLbLinear = new LinearLayout(mContext);
        rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 400);
        rvParams.setMargins(0, 10, 0, 0);
        menuLbLinear.setLayoutParams(rvParams);
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setCornerRadius(10);
        drawable.setColor(Color.rgb(200, 200, 200));
        menuLbLinear.setOrientation(LinearLayout.HORIZONTAL);
        menuLbLinear.setBackground(drawable);
        linearLayout.addView(menuLbLinear);

        RecyclerView menulbRv = new RecyclerView(mContext);
        rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        menulbRv.setLayoutParams(rvParams);
        menulbRv.setVisibility(View.GONE);
        menulbRv.setBackgroundColor(Color.rgb(240, 240, 240));
        menuLbLinear.addView(menulbRv);

        paiCaiLeftMenuLbAdapter = new PaiCaiLeftMenuLbAdapter(mContext);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        menulbRv.setLayoutManager(layoutManager);
        menulbRv.setAdapter(paiCaiLeftMenuLbAdapter);


        RecyclerView menuCirculationStart = new RecyclerView(mContext);
        rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        menuCirculationStart.setLayoutParams(rvParams);

        menuLbLinear.addView(menuCirculationStart);
        leftMenuAdapter = new LeftMenuAdapter(mContext);

        GridLayoutManager productLayoutManager = new GridLayoutManager(mContext, 4);
        productLayoutManager.setOrientation(RecyclerView.VERTICAL);
        menuCirculationStart.setLayoutManager(productLayoutManager);
        menuCirculationStart.setAdapter(leftMenuAdapter);
        menuCirculationStart.addItemDecoration(new MyGridSpacingItemDecoration(3));

        paiCaiLeftMenuLbAdapter.setOnUserClick(menuLBDto -> {
            leftMenuAdapter.setMenuLBDto(menuLBDto);
            leftMenuAdapter.showNotify();
            paiCaiLeftMenuLbAdapter.notifyDataSetChanged();
        });

        leftMenuAdapter.setOnUserClick(hosMenuDto -> {
            //点击返回，添加到列表中
            if (StringUtil.isEmpty(savedm)) {
                MsgDialog msgDialog = new MsgDialog(mContext);
                msgDialog.show();
                msgDialog.showMsg("当前菜谱尚未选择，请选择");
                return;
            }
            rightMenuAdapter.addMenu(hosMenuDto);
        });

        return linearLayout;
    }

    private View getRightView() {
        LinearLayout linearLayout = new LinearLayout(mContext);
        LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
        linearParams.setMargins(10, 10, 10, 10);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setLayoutParams(linearParams);

        LinearLayout topRv = new LinearLayout(mContext);
        topRv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        topRv.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.addView(topRv);

        TextView textView = new TextView(mContext);
        RelativeLayout.LayoutParams rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rvParams.addRule(RelativeLayout.CENTER_VERTICAL);
        rvParams.setMargins(10, 0, 10, 0);
        textView.setLayoutParams(rvParams);
        textView.setText("已排菜");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        topRv.addView(textView);


        TextView menuCirculationEndSortText = new TextView(mContext);
        linearParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
        linearParams.setMargins(5, 0, 5, 0);
        menuCirculationEndSortText.setId(View.generateViewId());
        menuCirculationEndSortText.setLayoutParams(linearParams);
        menuCirculationEndSortText.setHint("输入简码");
        menuCirculationEndSortText.setEnabled(false);
        menuCirculationEndSortText.setPadding(5, 5, 5, 5);
        menuCirculationEndSortText.setSingleLine(true);
        topRv.addView(menuCirculationEndSortText);


        TextView menuCirculationEndReset = new TextView(mContext);
        rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rvParams.setMargins(5, 0, 5, 0);
        rvParams.addRule(RelativeLayout.CENTER_VERTICAL);
        menuCirculationEndReset.setLayoutParams(rvParams);
        menuCirculationEndReset.setText("重置");
        menuCirculationEndReset.setPadding(20, 8, 20, 8);
        menuCirculationEndReset.setTextColor(Color.WHITE);
        menuCirculationEndReset.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        menuCirculationEndReset.setBackground(getResources().getDrawable(R.drawable.bg_btn_pay));
        topRv.addView(menuCirculationEndReset);

        TextView menuCirculationNotAll = new TextView(mContext);
        rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rvParams.setMargins(5, 0, 10, 0);
        menuCirculationNotAll.setId(View.generateViewId());
        menuCirculationNotAll.setLayoutParams(rvParams);
        menuCirculationNotAll.setText("全不排菜");
        menuCirculationNotAll.setPadding(20, 8, 20, 8);
        menuCirculationNotAll.setTextColor(Color.WHITE);
        menuCirculationNotAll.setBackground(getResources().getDrawable(R.drawable.bg_btn_pay));
        menuCirculationNotAll.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        topRv.addView(menuCirculationNotAll);

        ScrollView scrollView = new ScrollView(mContext);
        rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rvParams.setMargins(0, 5, 0, 0);
        scrollView.setLayoutParams(rvParams);
        linearLayout.addView(scrollView);

        GridViewForScrollView menu_circulationEndSort = new GridViewForScrollView(mContext);
        menu_circulationEndSort.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        menu_circulationEndSort.setNumColumns(13);
        menu_circulationEndSort.setHorizontalSpacing(5);
        menu_circulationEndSort.setVerticalSpacing(5);
        scrollView.addView(menu_circulationEndSort);

        LettersAdapter startLettersAdapter = new LettersAdapter(getActivity());
        menu_circulationEndSort.setAdapter(startLettersAdapter);


        LinearLayout weekMenuLinear = new LinearLayout(mContext);
        rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 400);
        rvParams.setMargins(0, 10, 0, 0);
        weekMenuLinear.setLayoutParams(rvParams);
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setCornerRadius(10);
        drawable.setColor(Color.rgb(200, 200, 200));
        weekMenuLinear.setBackground(drawable);
        weekMenuLinear.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.addView(weekMenuLinear);


        RecyclerView menuCirculationStart = new RecyclerView(mContext);
        menuCirculationStart.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 1));
        weekMenuLinear.addView(menuCirculationStart);
        rightMenuAdapter = new RightMenuAdapter(mContext);
        GridLayoutManager productLayoutManager = new GridLayoutManager(mContext, 4);
        productLayoutManager.setOrientation(RecyclerView.VERTICAL);
        menuCirculationStart.setLayoutManager(productLayoutManager);
        menuCirculationStart.setAdapter(rightMenuAdapter);
        menuCirculationStart.addItemDecoration(new MyGridSpacingItemDecoration(3));

        View lineView = new View(mContext);
        lineView.setLayoutParams(new RelativeLayout.LayoutParams(1, ViewGroup.LayoutParams.MATCH_PARENT));
        lineView.setBackgroundColor(Color.rgb(200, 200, 200));
        weekMenuLinear.addView(lineView);


        RecyclerView zlRv = new RecyclerView(mContext);
        rvParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        zlRv.setLayoutParams(rvParams);
        drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);

        float[] radius = new float[8];
        radius[0] = 0;
        radius[1] = 0;
        radius[2] = 10;
        radius[3] = 10;

        radius[4] = 10;
        radius[5] = 10;
        radius[6] = 0;
        radius[7] = 0;
        drawable.setCornerRadii(radius);
        drawable.setColor(Color.rgb(240, 240, 240));
        zlRv.setBackground(drawable);
        weekMenuLinear.addView(zlRv);

        paiCaiRightZlAdapter = new PaiCaiRightZlAdapter(mContext);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        zlRv.setLayoutManager(layoutManager);
        zlRv.setAdapter(paiCaiRightZlAdapter);

        paiCaiRightZlAdapter.setOnUserClick(bmzl -> {
            rightMenuAdapter.setZl(bmzl.getBm());
            rightMenuAdapter.showNotify();
        });

        rightMenuAdapter.setOnUserResult(nWeekMenuBakDtoList -> {
            leftMenuAdapter.setnWeekMenuBakDtoList(nWeekMenuBakDtoList);
            leftMenuAdapter.showNotify();
        });
        return linearLayout;
    }

    private String pinYinS(String str) {
        StringBuilder resStr = new StringBuilder();
        for (int i = 0; i < str.toCharArray().length; i++) {
            resStr.append(Pinyin.toPinyin(str.toCharArray()[i]).substring(0, 1));
        }
        return resStr.toString();
    }
}
